package com.hksj.Intelligentmanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.Intelligentmanagement.common.Rest;
import com.hksj.Intelligentmanagement.dto.*;
import com.hksj.Intelligentmanagement.entity.WnReadHandNumbness;
import com.hksj.Intelligentmanagement.service.SurgicalService;
import com.hksj.Intelligentmanagement.service.UserRegionService;
import com.hksj.Intelligentmanagement.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author @chenxingqing
 * @Version 1.0
 */
@Api(tags = "手术管理")
@RestController
@RequestMapping("/api/surgical")
public class SurgicalController {

    @Autowired
    private SurgicalService surgicalService;

    @Autowired
    private UserRegionService userRegionService;

    /**
     * 查询所有手术信息
     *
     * @return
     */
    @ApiOperation(value = "查询所有手术信息列表")
    @GetMapping("listAllSurgical")
    public Rest<List<SurgicalVo>> listAllSurgical(
        @ApiParam(value = "手术日期 yyyy-MM-dd",required = true) @RequestParam String time,
        @ApiParam(value = "主刀医生姓名",required = false) @RequestParam(required = false) String masterDoctorName,
        @ApiParam(value = "患者姓名",required = false) @RequestParam(required = false) String patientName,
        @ApiParam(value = "排班状态  0 = 未排班 1 = 已排班 2 = 排班中",required = false) @RequestParam(required = false) Integer schedulingStatus,
        @ApiParam(value = "区域id",required = false) @RequestParam(required = false) String regionId) {
        List<SurgicalVo> surgicalInfoVo = surgicalService.listAllSurgical(time,masterDoctorName,patientName,schedulingStatus,regionId);
        return Rest.ok(surgicalInfoVo);
    }

    @ApiOperation(value = "分页查询所有手术信息列表")
    @GetMapping("pageAllSurgical")
    public Rest<IPage<SurgicalVo>> pageAllSurgical(
        @ApiParam(value = "手术日期 yyyy-MM-dd",required = false) @RequestParam(required = false) String time,
        @ApiParam(value = "主刀医生姓名",required = false) @RequestParam(required = false) String masterDoctorName,
        @ApiParam(value = "排班状态  0 = 未排班 1 = 已排班 2 = 排班中",required = false) @RequestParam(required = false) Integer schedulingStatus,
        @ApiParam(value = "区域id",required = false) @RequestParam(required = false) String regionId,
        @ApiParam(value = "起始页",required = false,defaultValue = "1") @RequestParam(required = false,defaultValue = "1") Integer pageNum,
        @ApiParam(value = "每页大小",required = false,defaultValue = "10") @RequestParam(required = false,defaultValue = "10") Integer pageSize) {
        IPage<SurgicalVo> pageInfo = surgicalService.pageSurgical(new Page<>(pageNum,pageSize),time,masterDoctorName,schedulingStatus,regionId);
        return Rest.ok(pageInfo);
    }

    @ApiOperation(value = "查询已排班手术信息列表")
    @GetMapping("listScheduledSurgical")
    public Rest<RegionSurgicalVo> listScheduledSurgical(
        @ApiParam(value = "手术日期 yyyy-MM-dd",required = false) @RequestParam(required = false) String time,
        @ApiParam(value = "洗手护士/巡回护士姓名",required = false) @RequestParam(required = false) String nurseName,
        @ApiParam(value = "区域id",required = false) @RequestParam(required = false) String regionId) {
        RegionSurgicalVo regionSurgicalVo = surgicalService.getRegionSurgical(time,nurseName,regionId);
        return Rest.ok(regionSurgicalVo);
    }

    @ApiOperation(value = "提交排班信息")
    @PostMapping("schedulingSurgical")
    @PreAuthorize("hasAuthority('intelligentSchedule:operationSchedule:save')")
    public Rest schedulingSurgical(
        @RequestBody List<SurgicalRoomAndAssistantDTO> roomAndAssistants){
        surgicalService.batchSchedulingSurgical(roomAndAssistants);
        return Rest.ok();
    }

    @ApiOperation(value = "保存排班信息")
    @PostMapping("saveSchedulingSurgical")
    @PreAuthorize("hasAuthority('intelligentSchedule:operationSchedule:save')")
    public Rest saveSchedulingSurgical(
        @RequestBody SurgicalRoomAndAssistantDTO roomAndAssistants){
        surgicalService.schedulingSurgical(roomAndAssistants,true);
        return Rest.ok();
    }

    @ApiOperation(value = "取消排班信息")
    @GetMapping("cancelSchedulingSurgical")
    @PreAuthorize("hasAuthority('intelligentSchedule:operationSchedule:cancel')")
    public Rest cancelSchedulingSurgical(
        @ApiParam(value = "手术id",required = true) @RequestParam String surgicalId){
        surgicalService.cancelSchedulingSurgical(surgicalId);
        return Rest.ok();
    }

    @ApiOperation(value = "提交麻醉人员排班信息")
    @PostMapping("schedulingSurgicalForAnesthesia")
    @PreAuthorize("hasAuthority('intelligentSchedule:anaesthesiaSchedule:save')")
    public Rest schedulingSurgicalForAnesthesia(
        @RequestBody List<SurgicalRoomAndAssistantDTO> roomAndAssistants){
        surgicalService.batchSchedulingSurgicalForAnesthesia(roomAndAssistants);
        return Rest.ok();
    }

    @ApiOperation(value = "单个提交麻醉人员排班信息")
    @PostMapping("saveSchedulingSurgicalForAnesthesia")
    @PreAuthorize("hasAuthority('intelligentSchedule:anaesthesiaSchedule:save')")
    public Rest saveSchedulingSurgicalForAnesthesia(@RequestBody SurgicalRoomAndAssistantDTO roomAndAssistants){
        surgicalService.schedulingSurgicalForAnesthesia(roomAndAssistants,true);
        return Rest.ok();
    }


    @ApiOperation(value = "取消麻醉人员排班信息")
    @GetMapping("cancelSchedulingSurgicalForAnesthesia")
    @PreAuthorize("hasAuthority('intelligentSchedule:anaesthesiaSchedule:cancel')")
    public Rest cancelSchedulingSurgicalForAnesthesia(
        @ApiParam(value = "手术id",required = true) @RequestParam String surgicalId){
        surgicalService.cancelSchedulingSurgicalForAnesthesia(surgicalId);
        return Rest.ok();
    }

    @ApiOperation(value = "导出排班信息")
    @GetMapping("exportSchedulingSurgical")
    @PreAuthorize("hasAnyAuthority('intelligentSchedule:operationSchedule:export','intelligentSchedule:anaesthesiaSchedule:export')")
    public void exportSchedulingSurgical(
        @ApiParam(value = "手术日期 yyyy-MM-dd",required = false) @RequestParam(required = false) String time,
        @ApiParam(value = "区域id",required = false) @RequestParam(required = false) List<String> regionIds,
        HttpServletResponse response){
        surgicalService.exportSchedulingSurgical(time,regionIds,response);
    }

    @ApiOperation(value = "查询用户可排班的区域")
    @GetMapping("getRegionByUserId/{userId}")
    public Rest<List<UserRegionVo>> getRegionByUserId(@PathVariable String userId){
        List<UserRegionVo> regionVos = userRegionService.getRegionByUserId(userId);
        return Rest.ok(regionVos);
    }

    @ApiOperation(value = "保存用户排班区域")
    @PostMapping("/saveUserRegion")
    public Rest saveUserRegion(@RequestBody UserRegionDTO dto){
        userRegionService.saveUserRegion(dto);
        return Rest.ok();
    }

    @ApiOperation(value = "第三方数据-分页查询手术信息列表",response = WnReadHandNumbness.class)
    @PostMapping("pageThirdSurgical")
    public Rest<IPage<WnReadHandNumbness>> pageThirdSurgical(@RequestBody SurgicalQuery query){
        IPage<WnReadHandNumbness> page = surgicalService.pageThirdSurgical(query);
        return Rest.ok(page);
    }

    @ApiOperation(value = "第三方数据-查询排班信息")
    @PostMapping("/listThirdScheduling")
    public Rest<List<RoomSchedulingVO>> listThirdScheduling(@RequestBody SchedulingQuery query){
        List<RoomSchedulingVO> list = surgicalService.listScheduling(query);
        return Rest.ok(list);
    }

    @ApiOperation(value = "第三方数据-手术房间状态")
    @PostMapping("/listRoomSurgicalAndEnv")
    public Rest<List<RoomSurgicalAndEnvVO>> listRoomSurgicalAndEnv(@RequestBody SurgicalAndEnvQuery query){
        List<RoomSurgicalAndEnvVO> list = surgicalService.listRoomSurgicalAndEnv(query);
        return Rest.ok(list);
    }
}
